<?php
include_once "SqlArticleRepository.php";
include_once "Config.php";
include_once "SqlUserRepository.php";
class Application {
	private $contentRepository;
	private static $instance;
	private $userRepository;
	const ACTIVE_USER = "ACTIVE_USER";
	private function __construct() {
		$this->contentRepository = new SqlArticleRepository ( Config::DATABASE_URL, Config::DATABASE_PORT, Config::DATABASE_USERNAME, Config::DATABASE_PASSWORD );
		$this->userRepository = new SqlUserRepository ( Config::DATABASE_URL, Config::DATABASE_PORT, Config::DATABASE_USERNAME, Config::DATABASE_PASSWORD );
	}
	private function __clone() {
		// do nothing
	}
	public function getContentRepository() {
		return $this->contentRepository;
	}
	public static function getInstance() {
		if (! Application::$instance instanceof self) {
			session_start ();
			Application::$instance = new self ();
		}
		return Application::$instance;
	}
	public function getUserRepository() {
		return $this->userRepository;
	}
	public function setActiveUser($user) {
		if ($user === NULL) {
			session_unset ();
			session_destroy ();
		} else {
			$_SESSION [$this::ACTIVE_USER] = serialize ( $user );
		}
	}
	public function isActiveUserAvailable() {
		return isset ( $_SESSION [$this::ACTIVE_USER] );
	}
	public function getActiveUser() {
		if (! isset ( $_SESSION [$this::ACTIVE_USER] )) {
			return NULL;
		}
		return unserialize ( $_SESSION [$this::ACTIVE_USER] );
	}
}
?>
